# coding=utf-8
import os
from django.core.management import BaseCommand
from django.core.management.base import AppCommand

__author__ = 'daihaibin'

class Command(AppCommand):
    """
    初始化Manekineko项目物品数据到数据库
    """
    help = 'initial goods data for project Manekineko'
    args = '<appname goods_data_path ...>'

    def handle(self, *args, **options):
        if args and len(args) > 0:
            appname = args[0]
            print('prepare to initial %s data' % appname)
            from django.db import models
            app_model = models.get_app(appname)
            if len(args) > 1:
                data_path = args[1]
            else:
                data_path = os.path.join(os.path.dirname(os.path.abspath(app_model.__file__)), '%s_initial_data.xml' % appname)
            print(data_path)
            package = app_model.__package__
            package = '%s.%s' % (package, 'initial')
            m = __import__(package)
            packages = package.split('.')[1:]
            for p in packages:
                m = getattr(m, p)
            m = getattr(m,'XMLInitialHandler')
            m(data_path).initial()

